<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PersonalHistory extends Model
{
    protected $table = 'st_medical_personalhistory';

    protected $fillable = [
        'record_id','smoke','stop_smoke','drink','stop_drink','remark',
    ];

    public function medicalRecord()
    {
        return $this->belongsTo(MedicalRecord::class, 'record_id');
    }

    /**
     * 吸烟字段获取器
     * @param $value
     * @return string
     */
    public function getSmokeAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    /**
     * 饮酒字段获取器
     * @param $value
     * @return string
     */
    public function getDrinkAttribute($value)
    {
        return $value ? json_decode($value, true) : '';
    }

    /**
     * 吸烟字段修改器
     * @param $value
     */
    public function setSmokeAttribute($value)
    {
        $this->attributes['smoke'] = is_array( $value ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : $value;
    }

    /**
     * 饮酒字段修改器
     * @param $value
     */
    public function setDrinkAttribute($value)
    {
        $this->attributes['drink'] = is_array( $value ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : $value;
    }
}
